<link href='/static/packages/core/main.css' rel='stylesheet'/>
<link href='/static/packages/daygrid/main.css' rel='stylesheet'/>
<link href='/static/packages/timegrid/main.css' rel='stylesheet'/>
<link href='/static/packages/list/main.css' rel='stylesheet'/>
<script src='/static/packages/core/main.js'></script>
<script src='/static/packages/core/locales-all.js'></script>
<script src='/static/packages/interaction/main.js'></script>
<script src='/static/packages/bootstrap/main.js'></script>
<script src='/static/packages/daygrid/main.js'></script>
<script src='/static/packages/timegrid/main.js'></script>
<script src='/static/packages/list/main.js'></script>
<script src="/static/admin/video/jquery.js"></script>
<link href="/static/select/jquery.searchableSelect.css" rel="stylesheet" type="text/css">
<script src="/static/select/jquery.searchableSelect.js"></script>
<style>
    .external-event {
        padding: 10px 10px;
        font-weight: bold;
        margin-bottom: 4px;
        box-shadow: 0 1px 1px rgba(0, 0, 0, 0.1);
        text-shadow: 0 1px 1px rgba(0, 0, 0, 0.1);
        border-radius: 3px;
        cursor: move;
    }

    .hide {
        display: none !important;
    }

    .fc-button-primary {
        background-color: #18bc9c !important;
        border-color: #18bc9c !important;
    }

    /*.select {*/
    /*    display: inline-block;*/
    /*    width: 130px;*/
    /*    position: relative;*/
    /*    vertical-align: middle;*/
    /*    padding: 0;*/
    /*    overflow: hidden;*/
    /*    background-color: #fff;*/
    /*    color: #555;*/
    /*    border: 1px solid #aaa;*/
    /*    text-shadow: none;*/
    /*    border-radius: 4px;*/
    /*    transition: box-shadow 0.25s ease;*/
    /*    z-index: 2;*/
    /*}*/


    /*.select:hover {*/
    /*    box-shadow: 0 1px 4px rgba(0, 0, 0, 0.15);*/
    /*}*/

    /*.select:before {*/
    /*    content: "";*/
    /*    position: absolute;*/
    /*    width: 0;*/
    /*    height: 0;*/
    /*    border: 10px solid transparent;*/
    /*    border-top-color: #ccc;*/
    /*    top: 14px;*/
    /*    right: 10px;*/
    /*    cursor: pointer;*/
    /*    z-index: -2;*/
    /*}*/

    /*.select select {*/
    /*    cursor: pointer;*/
    /*    padding: 8px;*/
    /*    width: 100%;*/

    /*    border: none;*/
    /*    background: transparent;*/
    /*    background-image: none;*/
    /*    -webkit-appearance: none;*/
    /*    -moz-appearance: none;*/
    /*}*/

    /*.select select:focus {*/
    /*    outline: none;*/
    /*}*/

</style>
<div class="layui-row" id='wrap'>
    {if $verify_type eq 1}
    <div class="layui-fluid">
        <div class="layui-card">

            <div class="layui-card-body">
                <div style="padding-bottom: 10px;">
                        <button class="layui-btn layuiadmin-btn-admin layui-btn-sm" title="刷新" data-type="refresh"><i
                                class="layui-icon layui-icon-refresh-3"></i>
                        </button>
                    <button class="layui-btn layuiadmin-btn-admin layui-btn-sm {$auth->check('vacation/addVacation')?$auth->check('vacation/addVacation'):'hide'}"
                            title="导出" data-type="add"><i
                            class="layui-icon layui-icon-add-1"></i>新增
                    </button>
                    <button class="layui-btn layuiadmin-btn-admin layui-btn-sm {$auth->check('vacation/update')?$auth->check('vacation/update'):'hide'}"
                            title="编辑" data-type="edit"><i
                            class="layui-icon layui-icon-edit"></i>编辑
                    </button>
                    <button class="layui-btn layuiadmin-btn-admin layui-btn-sm {$auth->check('vacation/del')?$auth->check('vacation/del'):'hide'}"
                            title="删除" data-type="batchdel"><i
                            class="layui-icon layui-icon-add-delete"></i>删除
                    </button>
                    <!--                <button class="layui-btn layuiadmin-btn-admin layui-btn-sm {$auth->check('journal/del')?$auth->check('journal/del'):'hide'}"-->
                    <!--                        title="删除" data-type="batchdel"><i-->
                    <!--                        class="layui-icon layui-icon-delete"></i>删除-->
                    <!--                </button>-->
                </div>
                <table id="LAY-user-back-manage" lay-filter="LAY-user-back-manage"></table>
            </div>
        </div>
    </div>
    {else /}
    <div class="layui-col-xs12 layui-col-sm12 layui-col-md3">
        <div class="layui-fluid">
            <div class="{$auth->check('vacation/checkTable')?$auth->check('vacation/checkTable'):'hide'}">
                <fieldset class="layui-elem-field layui-field-title" style="margin-top: 30px;">
                    <legend>查看切换</legend>
                </fieldset>
                <blockquote class="layui-elem-quote layui-quote-nm">
                    <div> <button type="submit" id='agree' class="layui-btn layui-btn-fluid"
                                  style="background-color: #18bc9c !important; border-radius: 5px;">转换表格查看
                    </button></div>
                </blockquote>
            </div>
            <fieldset class="layui-elem-field layui-field-title" style="margin-top: 30px;">
                <legend>备选事件</legend>
            </fieldset>

            <blockquote class="layui-elem-quote layui-quote-nm">
                <div id='external-events-list'>
                    {foreach $alternative as $key=>$vo}
                    <div class='fc-event external-event' id="tip_{$vo.id}" onmouseover="tips({$vo.id})" data-id={$vo.id}
                         style="background-color:{$vo.color} !important; border:1px solid {$vo.color} !important;">
                        {$vo.title}
                    </div>

                    {/foreach}
                </div>
                <p>
                    <input type='checkbox' id='drop-remove'/>
                    <label for='drop-remove'>添加后移除事件</label>
                </p>
            </blockquote>


            <fieldset class="layui-elem-field layui-field-title" style="margin-top: 30px;">
                <legend>添加事件</legend>
            </fieldset>
            <blockquote class="layui-elem-quote layui-quote-nm">
                <form class="layui-form" action="">
                    <div class="layui-form-item">
                        <div class="layui-input-inline" style="margin:0px!important;">
                            <input type="text" value="" name="color" lay-verify="required" placeholder="请选择颜色"
                                   class="layui-input" id="test-form-input">
                        </div>
                        <div class="layui-inline">
                            <div id="test-form"></div>
                        </div>
                    </div>
                    <div class="layui-form-item">
                        <div class="layui-input-block" style="margin-left:0px !important;">
                            <input type="text" name="title" lay-verify="required" placeholder="事件标题,不能为空"
                                   autocomplete="off"
                                   class="layui-input">
                        </div>

                    </div>
                    <div class="layui-form-item">
                        <div class="layui-input-block" style="margin-left:0px !important;">
                            <input type="text" name="remark" lay-verify="required" placeholder="请输入内容"
                                   autocomplete="off"
                                   class="layui-input">
                        </div>

                    </div>
                    <div class="layui-form-item">
                        <div class="layui-input-block" style="margin-left:0px !important;">
                            <input type="radio" name="status" value="0" title="添加到备选事件" lay-filter="type" checked="">
                            <input type="radio" name="status" value="1" title="添加到日历" lay-filter="type">
                        </div>
                    </div>
                    <div class="layui-form-item" id="time" style="display: none;">
                        <div class="layui-input-block" style="margin-left:0px !important;">
                            <input type="text" name="time" id="period" placeholder="时间范围"
                                   autocomplete="off"
                                   class="layui-input">
                        </div>

                    </div>

                    <div class="layui-form-item">
                        <div class="layui-input-block" style="margin-left:0px !important;">
                            <button type="submit" class="layui-btn layui-btn-sm" lay-submit="" lay-filter="add">添加
                            </button>
                            <button type="reset" class="layui-btn layui-btn-primary layui-btn-sm">重置</button>
                        </div>
                    </div>
                </form>
            </blockquote>

        </div>
    </div>
    <div class="layui-col-xs12 layui-col-sm12 layui-col-md9">

        <div class="layui-fluid">
            <div class="select" id="theme-system-selector" style="float: right;">
                <select id="admin_id">
                    <option value="0">请选择</option>
                    {foreach $admin as $key=>$vo}
                    <option value="{$vo.id}">{$vo.nickname}</option>
                    {/foreach}
                </select>
            </div>

            <div style="margin-top:10px"></div>
            <div id='calendar' style="max-height: 200px !important;"></div>
            <div style='clear:both'></div>
        </div>
    </div>
    {/if}

</div>
<script src="/static/layuiadmin/layui/layui.js"></script>
<script>
    var addVacation = "{:url('vacation/addVacation')}", VacationDetail = "{:url('vacation/VacationDetail')}",
        deleteUrl = "{:url('vacation/del')}", update = "{:url('vacation/update')}", backupdate = "{:url('vacation/backupdate')}",
        flowchart = "{:url('Common/flowchart')}";
    layui.config({
        base: '/static/layuiadmin/' //静态资源所在路径
    }).extend({
        index: 'lib/index' //主入口模块
    }).use(['index', 'form', 'table','laydate', 'soulTable', 'colorpicker'], function () {
        var $ = layui.$,
            form = layui.form
            , layer = layui.layer
            , colorpicker = layui.colorpicker, laydate = layui.laydate;
        form.render();

        if ("{$verify_type}" == 1){
            form.render();
            var listUrl = "{:url('vacation/checkTable')}";
            var $ = layui.$
                , form = layui.form
                , table = layui.table,laydate = layui.laydate;
            table.render({
                elem: "#LAY-user-back-manage", url: listUrl
                , cols: [
                    [
                        {type: "checkbox", fixed: "left"},
                        {field: "alternative_title", align: "center", title: "类型"},
                        {field: "username", align: "center", title: "人员名称"},
                    ]],
                response: {
                    statusCode: 200 //重新规定成功的状态码为 200，table 组件默认为 0
                },
                parseData: function (res) { //将原始数据解析成 table 组件所规定的数据
                    return {
                        "code": res.code,
                        "count": res.total, //解析数据长度
                        "data": res.data, //解析数据列表
                    };

                },
                text: {
                    none: '呀！一条数据都没有'
                },

                page: true,
                limit: 10,
            });
            //监听搜索
            form.on('submit(LAY-user-back-search)', function (data) {
                var field = data.field;
                //执行重载
                table.reload('LAY-user-back-manage', {
                    where: field
                });
            });
            //事件
            var active = {
                refresh: function () {
                    table.reload('LAY-user-back-manage');
                },
                add: function () {
                    layer.open({
                        type: 2
                        , skin: 'layui-layer-molv'
                        , title: '填写'
                        , content: addVacation
                        , area: ['100%', '100%']
                        , maxmin: true
                        , btn: ['确定', '取消']
                        , yes: function (index, layero) {
                            var iframeWindow = window['layui-layer-iframe' + index]
                                , submitID = 'LAY-user-back-submit'
                                , submit = layero.find('iframe').contents().find('#' + submitID);
                            //监听提交
                            iframeWindow.layui.form.on('submit(' + submitID + ')', function (data) {
                                var field = data.field; //获取提交的字段
                                if (field.flow_id == "") {
                                    layer.msg("请选择工作流")
                                    return false;
                                }
                                if (field.alternative_title == "") {
                                    layer.msg("请选择休假类型")
                                    return false;
                                }
                                if (field.alternative_title == "调休") {
                                    // if (field.dy_end_time == "" && field.dy_start_time == "") {
                                    //     layer.msg("调休对应时间不能为空")
                                    //     return false;
                                    // }
                                    // if (new Date(field.dy_start_time) > new Date(field.dy_end_time)) {
                                    //     layer.msg("调休对应开始时间不能大于调休对应结束时间")
                                    //     return false;
                                    // }
                                    if (field.select == ""){
                                        layer.msg("请选择对应的加班单");
                                        return false;
                                    }
                                }
                                if (new Date(field.start_time) > new Date(field.end_time)) {
                                    layer.msg("开始时间不能大于结束时间")
                                    return false;
                                }

                                //提交 Ajax 成功后，静态更新表格中的数据
                                $.ajax({
                                    url: addVacation,
                                    type: "post",
                                    typeData: "json",
                                    data: field,
                                    // beforeSend: function () {
                                    //     layer.load(2, {
                                    //         shade: [0.6, '#fff'], content: '数据新增中...', success: function (layero) {
                                    //             layero.find('.layui-layer-content').css({
                                    //                 'padding-top': '6px',
                                    //                 'width': '150px',
                                    //                 'padding-left': '40px'
                                    //             });
                                    //             layero.find('.layui-layer-ico16, .layui-layer-loading .layui-layer-loading2').css({
                                    //                 'width': '150px !important',
                                    //                 'background-position': '2px 0 !important'
                                    //             });
                                    //         }
                                    //     });
                                    // },
                                    success: function (res) {
                                        if (res.code === 200) {
                                            layer.msg(res.msg, {icon: 1});
                                            location.reload();
                                            layer.closeAll(); //关闭弹层
                                        } else {
                                            layer.msg(res.msg, {icon: 5});
                                            return false;
                                        }
                                    }
                                });
                            });
                            submit.trigger('click');
                        }
                    });
                }
                ,batchdel: function () {
                    var checkStatus = table.checkStatus('LAY-user-back-manage')
                        , checkData = checkStatus.data; //得到选中的数据
                    if (checkData.length === 0) {
                        return layer.msg('请选择数据');
                    }
                    var id = ''; //task_id
                    for (var i = 0; i < checkData.length; i++) {
                        id += checkData[i].id + ",";
                    }
                    id = id.substring(0, id.length - 1);
                    var ids = $.param({'ids': id});
                    layer.confirm('确定删除吗？', function (index) {
                        //执行 Ajax 后重载
                        $.ajax({
                            url: deleteUrl,
                            type: "post",
                            data: ids,
                            success: function (res) {
                                if (res.code === 200) {
                                    layer.msg(res.msg, {icon: 1});
                                    table.reload('LAY-user-back-manage');
                                    layer.close(index); //关闭弹层
                                } else {
                                    console.log(res);
                                    layer.msg(res.msg, {icon: 5});
                                    return false;
                                }
                            }
                        });
                        // table.reload('LAY-user-back-manage');
                        // layer.msg('已删除');
                    });
                }
                ,edit: function () {
                    var checkStatus = table.checkStatus('LAY-user-back-manage')
                        , checkData = checkStatus.data; //得到选中的数据
                    if (checkData.length === 0) {
                        return layer.msg('请选择数据');
                    }
                    if (checkData.length > 1) {
                        return layer.msg('只能选择一条数据');
                    }
                    var id = ''; //task_id
                    for (var i = 0; i < checkData.length; i++) {
                        id += checkData[i].id + ",";
                    }
                    id = id.substring(0, id.length - 1);
                    var param = $.param({'id': id});
                    layer.open({
                        type: 2
                        , skin: 'layui-layer-molv'
                        , title: '编辑'
                        , content: VacationDetail + '?' + param
                        , area: ['100%', '100%']
                        , maxmin: true
                        , btn: ['确定', '查看流程图','取消']
                        , yes: function (index, layero) {
                            var iframeWindow = window['layui-layer-iframe' + index]
                                , submitID = 'LAY-user-back-submit'
                                , submit = layero.find('iframe').contents().find('#' + submitID);
                            //监听提交
                            iframeWindow.layui.form.on('submit(' + submitID + ')', function (data) {
                                var field = data.field; //获取提交的字段
                                //提交 Ajax 成功后，静态更新表格中的数据
                                $.ajax({
                                    url: update + '?' + param,
                                    type: "post",
                                    typeData: "json",
                                    data: field,
                                    success: function (res) {
                                        if (res.code === 200) {
                                            layer.msg(res.msg, {icon: 1});
                                            location.reload();
                                            layer.closeAll(); //关闭弹层
                                        } else {
                                            layer.msg(res.msg, {icon: 5});
                                            return false;
                                        }
                                    }
                                });
                            });
                            submit.trigger('click');
                        }
                        ,btn2: function (index, layero) {
                            var params = $.param({'id': id, 'table': 'vacations'});
                            layer.open({
                                type: 2
                                , skin: 'layui-layer-molv'
                                , title: '查看流程图'
                                , content: flowchart + '?' + params
                                , area: ['100%', '100%']
                                , maxmin: true
                            });
                            return false;
                        }
                    });
                }
            };
            $('.layui-btn.layuiadmin-btn-admin').on('click', function () {
                var type = $(this).data('type');
                active[type] ? active[type].call(this) : '';
            });
        }

        //表单赋值
        colorpicker.render({
            elem: '#test-form'
            , color: '#1c97f5'
            , done: function (color) {
                $('#test-form-input').val(color);
            }
        });
        //监听按钮
        form.on('submit(add)', function (data) {
            $.ajax({
                url: "{:url('vacation/addAlternative')}",
                data: data.field,
                type: "post",
                typeData: "json",
                success: function (res) {
                    if (res.code === 200) {
                        layer.msg(res.msg, {icon: 1});
                        location.reload();
                        layer.closeAll(); //关闭弹层
                    } else {
                        layer.msg(res.msg, {icon: 5});
                        return false;
                    }
                }
            });
            return false;
        });
        //监听radio
        form.on('radio(type)', function (data) {
            if (data.value == 1) {
                $('#time').show();
            } else {
                $('#time').hide();
            }
        });
        //日期范围
        laydate.render({
            elem: '#period'
            , range: '~'
            , type: 'datetime'
            , min: Date()
            , trigger: 'click'//呼出事件改成click
        });
        var html = document.getElementById('theme-system-selector');
        $('.fc-left').append(html);

    });

    function tips(obj) {
        $.ajax({
            url: "{:url('vacation/tips')}",
            type: "post",
            dataType: "json",
            data: {'id': obj},
            success: function (res) { // 获取当前月的数据
                if (res.code == 200) {
                    layer.tips(res.msg, '#tip_' + obj, {
                        tips: [1, '#0FA6D8'] //还可配置颜色
                    });
                } else {
                    layer.tips(res.msg, '#tip_' + obj, {
                        tips: [1, '#0FA6D8'] //还可配置颜色
                    });
                }
            }
        });
    }
    if ("{$verify_type}" == 2){
        document.addEventListener('DOMContentLoaded', function () {
            var initialLocaleCode = 'zh-cn';
            var localeSelectorEl = document.querySelector('#theme-system-selector select');
            var Draggable = FullCalendarInteraction.Draggable;
            var containerEl = document.getElementById('external-events-list');
            new Draggable(containerEl, {
                itemSelector: '.fc-event',
                eventData: function (eventEl) {
                    return {
                        title: eventEl.innerText.trim()
                    }
                }
            });

            var calendarEl = document.getElementById('calendar');
            var calendar;
            calendar = new FullCalendar.Calendar(calendarEl, {
                plugins: ['bootstrap', 'interaction', 'dayGrid', 'timeGrid', 'list'],
                eventSources: [
                    "{:url('administration/vacation')}",
                ],
                header: {
                    left: 'prev,next today myCustomButton',
                    center: 'title',
                    right: 'dayGridMonth,timeGridWeek,timeGridDay,listMonth'
                },
                editable: true,
                firstDay: 1,//设置一周中显示的第一天是哪天，周日是0，周一是1，类推
                weekMode: 'fixed',//在月视图里显示周的模式，因为每月周数可能不同，所以月视图高度不一定。fixed：固定显示6周高，日历高度保持不变liquid：不固定周数，高度随周数变化variable：不固定周数，但高度固定
                weekNumbers: true,//是否在日历中显示周次(一年中的第几周)，如果设置为true，则会在月视图的左侧、周视图和日视图的左上角显示周数。
                weekNumberCalculation: 'iso',//周次的显示格式。
                weekNumbersWithinDays: true,
                aspectRatio: 1.46,//设置日历单元格宽度与高度的比例。
                handleWindowResize: true,//是否随浏览器窗口大小变化而自动变化。
                businessHours: true,
                theme: true,
                weekNumberTitle: 'UTF-8',
                eventLimit: true,
                nowIndicator: true,            //周/日视图中显示今天当前时间点（以红线标记），默认false不显示
                navLinks: true,
                locale: initialLocaleCode,
                selectable: true,
                selectMirror: true,
                eventResizableFromStart: true,
                events: function (info, successCallback, failureCallback) {
                    $.ajax({
                        url: "{:url('administration/vacation')}",
                        type: "post",
                        dataType: "json",
                        data: {'admin_id': $('#admin_id').val()},
                        success: function (res) { // 获取当前月的数据
                            var events = [];
                            $.each(res, function (i, c) {
                                events.push({
                                    id: c.id,
                                    title: c.title,
                                    start: c.start, // will be parsed
                                    end: c.end,
                                    //allDay: true,
                                    constraint: c.remark,
                                    color: c.color,
                                    zt: c.status,
                                    task_status: c.task_status,
                                    status:c.status
                                });
                            });
                            successCallback(events);
                        }
                    });
                },
                drop: function (arg) {

                    var id = arg.draggedEl.getAttribute('data-id'); //id
                    var background_color = arg.draggedEl.style.backgroundColor;
                    var title = arg.draggedEl.innerText.trim(); //标题
                    var date_time = arg.dateStr;
                    var param = $.param({
                        'id': id,
                        'title': title,
                        'start_time': date_time,
                        'end_time': date_time,
                        'background_color': background_color
                    });
                    if (document.getElementById('drop-remove').checked) {
                        $.ajax({
                            url: "{:url('Vacation/del_type')}",
                            type: "post",
                            typeData: "json",
                            data: {"id": id},
                            success: function (res) {
                                if (res.code === 200) {
                                    layer.open({
                                        type: 2
                                        , skin: 'layui-layer-molv'
                                        , title: title + '填写'
                                        , content: addVacation + '?' + param
                                        , area: ['50%', '60%']
                                        , maxmin: true
                                        , btn: ['确定', '取消']
                                        , yes: function (index, layero) {
                                            var iframeWindow = window['layui-layer-iframe' + index]
                                                , submitID = 'LAY-user-back-submit'
                                                , submit = layero.find('iframe').contents().find('#' + submitID);
                                            //监听提交
                                            iframeWindow.layui.form.on('submit(' + submitID + ')', function (data) {
                                                var field = data.field; //获取提交的字段
                                                if (field.flow_id == "") {
                                                    layer.msg("请选择工作流")
                                                    return false;
                                                }
                                                if (field.alternative_title == "") {
                                                    layer.msg("请选择休假类型")
                                                    return false;
                                                }
                                                if (field.alternative_title == "调休") {
                                                    // if (field.dy_end_time == "" && field.dy_start_time == "") {
                                                    //     layer.msg("调休对应时间不能为空")
                                                    //     return false;
                                                    // }
                                                    // if (new Date(field.dy_start_time) > new Date(field.dy_end_time)) {
                                                    //     layer.msg("调休对应开始时间不能大于调休对应结束时间")
                                                    //     return false;
                                                    // }
                                                    if (field.select == ""){
                                                        layer.msg("请选择对应的加班单");
                                                        return false;
                                                    }
                                                }
                                                if (new Date(field.start_time) > new Date(field.end_time)) {
                                                    layer.msg("开始时间不能大于结束时间")
                                                    return false;
                                                }
                                                // 提交
                                                // Ajax
                                                // 成功后，静态更新表格中的数据
                                                $.ajax({
                                                    url: addVacation,
                                                    type: "post",
                                                    typeData: "json",
                                                    data: field,
                                                    // beforeSend: function () {
                                                    //     layer.load(2, {
                                                    //         shade: [0.6, '#fff'],
                                                    //         content: '数据新增中...',
                                                    //         success: function (layero) {
                                                    //             layero.find('.layui-layer-content').css({
                                                    //                 'padding-top': '6px',
                                                    //                 'width': '150px',
                                                    //                 'padding-left': '40px'
                                                    //             });
                                                    //             layero.find('.layui-layer-ico16, .layui-layer-loading .layui-layer-loading2').css({
                                                    //                 'width': '150px !important',
                                                    //                 'background-position': '2px 0 !important'
                                                    //             });
                                                    //         }
                                                    //     });
                                                    // },
                                                    success: function (res) {
                                                        if (res.code === 200) {
                                                            layer.msg(res.msg, {icon: 1});
                                                            location.reload();
                                                            layer.closeAll(); //关闭弹层
                                                        } else {
                                                            layer.msg(res.msg, {icon: 5});
                                                            return false;
                                                        }
                                                    }
                                                });
                                            });
                                            submit.trigger('click');
                                        }
                                    });
                                } else {
                                    layer.msg(res.msg, {icon: 5});
                                    return false;
                                }
                            }
                        });
                    } else {
                        layer.open({
                            type: 2
                            , skin: 'layui-layer-molv'
                            , title: title + '填写'
                            , content: addVacation + '?' + param
                            , area: ['100%', '100%']
                            , maxmin: true
                            , btn: ['确定', '取消']
                            , yes: function (index, layero) {
                                var iframeWindow = window['layui-layer-iframe' + index]
                                    , submitID = 'LAY-user-back-submit'
                                    , submit = layero.find('iframe').contents().find('#' + submitID);
                                //监听提交
                                iframeWindow.layui.form.on('submit(' + submitID + ')', function (data) {
                                    var field = data.field; //获取提交的字段
                                    if (field.flow_id == "") {
                                        layer.msg("请选择工作流")
                                        return false;
                                    }
                                    if (field.alternative_title == "") {
                                        layer.msg("请选择休假类型")
                                        return false;
                                    }
                                    if (field.alternative_title == "调休") {
                                        // if (field.dy_end_time == "" && field.dy_start_time == "") {
                                        //     layer.msg("调休对应时间不能为空")
                                        //     return false;
                                        // }
                                        // if (new Date(field.dy_start_time) > new Date(field.dy_end_time)) {
                                        //     layer.msg("调休对应开始时间不能大于调休对应结束时间")
                                        //     return false;
                                        // }
                                        if (field.select == ""){
                                            layer.msg("请选择对应的加班单");
                                            return false;
                                        }
                                    }
                                    if (new Date(field.start_time) > new Date(field.end_time)) {
                                        layer.msg("开始时间不能大于结束时间")
                                        return false;
                                    }

                                    //提交 Ajax 成功后，静态更新表格中的数据
                                    $.ajax({
                                        url: addVacation,
                                        type: "post",
                                        typeData: "json",
                                        data: field,
                                        // beforeSend: function () {
                                        //     layer.load(2, {
                                        //         shade: [0.6, '#fff'], content: '数据新增中...', success: function (layero) {
                                        //             layero.find('.layui-layer-content').css({
                                        //                 'padding-top': '6px',
                                        //                 'width': '150px',
                                        //                 'padding-left': '40px'
                                        //             });
                                        //             layero.find('.layui-layer-ico16, .layui-layer-loading .layui-layer-loading2').css({
                                        //                 'width': '150px !important',
                                        //                 'background-position': '2px 0 !important'
                                        //             });
                                        //         }
                                        //     });
                                        // },
                                        success: function (res) {
                                            if (res.code === 200) {
                                                layer.msg(res.msg, {icon: 1});
                                                location.reload();
                                                layer.closeAll(); //关闭弹层
                                            } else {
                                                layer.msg(res.msg, {icon: 5});
                                                return false;
                                            }
                                        }
                                    });
                                });
                                submit.trigger('click');
                            }
                        });
                    }

                },
                eventClick: function (info) {
                    var title = info.event.title;
                    var param = $.param({'id': info.event.id});
                    layer.open({
                        type: 2
                        , skin: 'layui-layer-molv'
                        , title: title + '查看'
                        , content: VacationDetail + '?' + param
                        , area: ['100%', '100%']
                        , maxmin: true
                        , btn: ['更新','删除', '查看流程图','退回后更新']
                        , yes: function (index, layero) {
                            var iframeWindow = window['layui-layer-iframe' + index]
                                , submitID = 'LAY-user-back-submit'
                                , submit = layero.find('iframe').contents().find('#' + submitID);
                            //监听提交
                            iframeWindow.layui.form.on('submit(' + submitID + ')', function (data) {
                                var field = data.field; //获取提交的字段
                                //提交 Ajax 成功后，静态更新表格中的数据
                                $.ajax({
                                    url: update + '?' + param,
                                    type: "post",
                                    typeData: "json",
                                    data: field,
                                    success: function (res) {
                                        if (res.code === 200) {
                                            layer.msg(res.msg, {icon: 1});
                                            location.reload();
                                            layer.closeAll(); //关闭弹层
                                        } else {
                                            layer.msg(res.msg, {icon: 5});
                                            return false;
                                        }
                                    }
                                });
                            });
                            submit.trigger('click');
                        }
                        , btn2: function (index, layero) {
                            var iframeWindow = window['layui-layer-iframe' + index]
                                , submitID = 'LAY-user-back-submit'
                                , submit = layero.find('iframe').contents().find('#' + submitID);
                            //监听提交
                            iframeWindow.layui.form.on('submit(' + submitID + ')', function (data) {
                                var field = data.field; //获取提交的字段
                                //提交 Ajax 成功后，静态更新表格中的数据
                                layer.confirm("你确定删除该事件吗？", function (t) {
                                    $.ajax({
                                        url: deleteUrl + '?' + param, success: function (res) {
                                            if (res.code === 200) {
                                                layer.msg(res.msg, {icon: 1});
                                                location.reload();
                                                layer.close(t); //关闭弹层
                                            } else {
                                                layer.msg(res.msg, {icon: 5});
                                                return false;
                                            }
                                        }
                                    });
                                })
                            });
                            submit.trigger('click');
                        }, btn3: function (index, layero) {
                            var params = $.param({'id': info.event.id, 'table': 'vacations'});
                            layer.open({
                                type: 2
                                , skin: 'layui-layer-molv'
                                , title: '查看流程图'
                                , content: flowchart + '?' + params
                                , area: ['100%', '100%']
                                , maxmin: true
                            });
                            return false;
                        },btn4(index, layero){
                            var iframeWindow = window['layui-layer-iframe' + index]
                                , submitID = 'LAY-user-back-submit'
                                , submit = layero.find('iframe').contents().find('#' + submitID);
                            //监听提交
                            iframeWindow.layui.form.on('submit(' + submitID + ')', function (data) {
                                var field = data.field; //获取提交的字段
                                //提交 Ajax 成功后，静态更新表格中的数据
                                $.ajax({
                                    url: backupdate + '?' + param,
                                    type: "post",
                                    typeData: "json",
                                    data: field,
                                    success: function (res) {
                                        if (res.code === 200) {
                                            layer.msg(res.msg, {icon: 1});
                                            location.reload();
                                            layer.closeAll(); //关闭弹层
                                        } else {
                                            layer.msg(res.msg, {icon: 5});
                                            return false;
                                        }
                                    }
                                });
                            });
                            submit.trigger('click');
                            return false;
                        }
                        , success: function (layero, index) {
                            if (info.event.extendedProps.zt == 1 && info.event.extendedProps.task_status < 2) { //进行中 暂未审核
                                //0 更新 1 删除 2 查看流程图 3 退回后编辑
                                $(".layui-layer-btn0").css("display", "black");
                                $(".layui-layer-btn1").css("display", "black");
                                $(".layui-layer-btn2").css("display", "none");
                                $(".layui-layer-btn3").css("display", "none");
                            } else if (info.event.extendedProps.zt == -1 && info.event.extendedProps.task_status >= 2) { //退回
                                $(".layui-layer-btn3").css("display", "black");
                                $(".layui-layer-btn2").css("display", "black");
                                $(".layui-layer-btn0").css("display", "none");
                                $(".layui-layer-btn1").css("display", "none");
                            }else if (info.event.extendedProps.zt == 1 && info.event.extendedProps.task_status >= 2){ //审核中
                                $(".layui-layer-btn2").css("display", "black");
                                $(".layui-layer-btn0").css("display", "none");
                                $(".layui-layer-btn1").css("display", "none");
                                $(".layui-layer-btn3").css("display", "none");
                            }else if (info.event.extendedProps.zt == 2 && info.event.extendedProps.task_status >= 2){
                                $(".layui-layer-btn2").css("display", "black");
                                $(".layui-layer-btn0").css("display", "none");
                                $(".layui-layer-btn1").css("display", "none");
                                $(".layui-layer-btn3").css("display", "none");
                            }else{
                                console.log(5)
                            }
                            $(".layui-layer-btn1").css("color", "white");
                            $(".layui-layer-btn2").css("color", "white");
                            $(".layui-layer-btn3").css("color", "white");
                            $(".layui-layer-btn0").addClass("{$auth->check('vacation/update')?$auth->check('vacation/update'):'hide'}");
                            $(".layui-layer-btn1").addClass("{$auth->check('vacation/del')?$auth->check('vacation/del'):'hide'}");
                            $(".layui-layer-btn2").addClass("{$auth->check('common/flowchart')?$auth->check('common/flowchart'):'hide'}");
                            $(".layui-layer-btn3").addClass("{$auth->check('vacation/backupdate')?$auth->check('vacation/backupdate'):'hide'}");

                        }
                    });
                },
                eventDrop: function (info) {
                    var start = new Date(info.event.start);
                    var start_time = start.getFullYear() + '-' + formatTen(start.getMonth() + 1) + '-' + formatTen(start.getDate()) + ' ' + formatTen(start.getHours()) + ':' + formatTen(start.getMinutes()) + ':' + formatTen(start.getSeconds());
                    var end = new Date(info.event.end);
                    var end_time = end.getFullYear() + '-' + formatTen(end.getMonth() + 1) + '-' + formatTen(end.getDate()) + ' ' + formatTen(end.getHours()) + ':' + formatTen(end.getMinutes()) + ':' + formatTen(end.getSeconds());
                    $.ajax({
                        url: "{:url('vacation/eventDrop')}",
                        type: "post",
                        typeData: "json",
                        data: {"id": info.event.id, 'start_time': start_time, 'end_time': end_time},
                        success: function (res) {
                            if (res.code === 200) {
                                layer.msg(res.msg, {icon: 1});
                                location.reload();
                                layer.closeAll(); //关闭弹层
                            } else {
                                layer.msg(res.msg, {icon: 5});
                                return false;
                            }
                        }
                    });
                },
                eventMouseEnter: function (info) { //鼠标滑过
                    var html = '<div style="  width: 100%;  \n' +
                        '    height: auto;  \n' +
                        '    word-wrap:break-word;  \n' +
                        '    word-break:break-all;  \n' +
                        '    overflow: hidden;  padding:5px 10px; text-align:justify; line-height: 22px;color: #ffffff">\n' +
                        '<div style="margin-bottom: 20px;font-weight: bold;padding-bottom: 5px"><h4 class="text-danger">' + info.event.title + '</h4></div>\n' +
                        '<div style="font-size: 12px">' + info.event.constraint + '</div>\n' +
                        '</div>\n';
                    layer.tips(html, info.el, {tips: [3, info.event.backgroundColor]});
                },
                select: function (info) { //点击空白处
                    var start = new Date(info.start);
                    var start_time = start.getFullYear() + '-' + formatTen(start.getMonth() + 1) + '-' + formatTen(start.getDate());
                    var end = new Date(info.end);
                    var end_time = end.getFullYear() + '-' + formatTen(end.getMonth() - +1) + '-' + formatTen(end.getDate());
                    var param = $.param({'start_time': info.startStr, 'end_time': info.endStr});
                    layer.open({
                        type: 2
                        , skin: 'layui-layer-molv'
                        , title: '填写'
                        , content: addVacation + '?' + param
                        , area: ['100%', '100%']
                        , maxmin: true
                        , btn: ['确定', '取消']
                        , yes: function (index, layero) {
                            var iframeWindow = window['layui-layer-iframe' + index]
                                , submitID = 'LAY-user-back-submit'
                                , submit = layero.find('iframe').contents().find('#' + submitID);
                            //监听提交
                            iframeWindow.layui.form.on('submit(' + submitID + ')', function (data) {
                                var field = data.field; //获取提交的字段
                                if (field.flow_id == "") {
                                    layer.msg("请选择工作流")
                                    return false;
                                }
                                if (field.alternative_title == "") {
                                    layer.msg("请选择休假类型")
                                    return false;
                                }
                                if (field.alternative_title == "调休") {
                                    // if (field.dy_end_time == "" && field.dy_start_time == "") {
                                    //     layer.msg("调休对应时间不能为空")
                                    //     return false;
                                    // }
                                    // if (new Date(field.dy_start_time) > new Date(field.dy_end_time)) {
                                    //     layer.msg("调休对应开始时间不能大于调休对应结束时间")
                                    //     return false;
                                    // }
                                    if (field.select == ""){
                                        layer.msg("请选择对应的加班单");
                                        return false;
                                    }
                                }
                                if (new Date(field.start_time) > new Date(field.end_time)) {
                                    layer.msg("开始时间不能大于结束时间")
                                    return false;
                                }

                                $.ajax({
                                    url: addVacation,
                                    type: "post",
                                    typeData: "json",
                                    data: field,
                                    // beforeSend: function () {
                                    //     layer.load(2, {
                                    //         shade: [0.6, '#fff'], content: '数据新增中...', success: function (layero) {
                                    //             layero.find('.layui-layer-content').css({
                                    //                 'padding-top': '6px',
                                    //                 'width': '150px',
                                    //                 'padding-left': '40px'
                                    //             });
                                    //             layero.find('.layui-layer-ico16, .layui-layer-loading .layui-layer-loading2').css({
                                    //                 'width': '150px !important',
                                    //                 'background-position': '2px 0 !important'
                                    //             });
                                    //         }
                                    //     });
                                    // },
                                    success: function (res) {
                                        if (res.code === 200) {
                                            layer.msg(res.msg, {icon: 1});
                                            location.reload();
                                            layer.closeAll(); //关闭弹层
                                        } else {
                                            layer.msg(res.msg, {icon: 5});
                                            return false;
                                        }
                                    }
                                });
                            });
                            submit.trigger('click');
                        }
                    });
                },
            });
            calendar.render();
            $('#admin_id').on("change", function () {
                calendar.refetchEvents();
            })

        });
    }

    $(function () {
        $('#admin_id').searchableSelect();
    });

    function formatTen(num) {
        return num > 9 ? (num + "") : ("0" + num);
    }
    var url = "{:url('vacation/checkTable')}"
    //转换表格查看
    $('#agree').on('click', function (res) {
        layer.open({
            type: 2
            , title: "请假表格查看"
            , content: url
            , area: ['100%', '100%']
            , maxmin: true
            // , btn: ['确定', '流程图', '取消']
            // , success: function (res) {
            //     console.log(data.instancestatus);
            // }
        });
    });

</script>
